import random
class Player:
    def __init__(self, name):
        self.name = name
        self.room = None
        self.wins = 0
    def fight(self, other):
        choices = [1, 2, 3]
        p1_choice = int(random.choice(choices))
        p2_choice = int(random.choice(choices))
        print(f"{self.name} 出的是 {p1_choice},{other.name} 出的是 {p2_choice}")
        if p1_choice == p2_choice:
            return "平局"
        if (p1_choice - p2_choice == 1) or (p1_choice - p2_choice == -2):
            self.wins += 1
            return f"{self.name} 赢"
        else:
            other.wins += 1
            return f"{other.name} 赢"
class Room:
    def __init__(self, room_id):
        self.room_id = room_id
        self.players = []
def create_room():
    room_id = input("请输入房间号:")
    room = Room(room_id)
    print(f"创建房间 {room.room_id} 成功")
    rooms.append(room)
    return room
def create_player():
    name = input("请输入角色名:")
    player = Player(name)
    print(f"创建角色 {player.name} 成功")
    players.append(player)
    return player
def join_room(player, room):
    room.players.append(player)
    print(f"{player.name} 加入房间 {room.room_id} 成功")

rooms = []
players = []
while True:
    choice = input("1. 创建房间 2. 创建角色 3. 将角色加入房间 4. 选择房间战斗 5. 退出")
    if choice == "1":
        room = create_room()
    elif choice == "2":
        player = create_player()
    elif choice == "3":
        room_ids = [r.room_id for r in rooms]
        print(f"现有房间：{room_ids}")
        room_id = input("请选择房间ID：")
        for r in rooms:
            if r.room_id == room_id:
                room = r
                break
        player_names = [p.name for p in players]
        print(f"现有玩家：{player_names}")
        player_name = input("请选择玩家名字：")
        player = next((p for p in players if p.name == player_name), None)
        join_room(player, room)
        print(f"将{player_name}加入房间{room_id}成功")
    elif choice == "4":
        room_ids = ", ".join(r.room_id for r in rooms)
        room_id = input(f"请从以下房间选择要进入的房间号：{room_ids}")
        for r in rooms:
            if r.room_id == room_id:
                room = r
                break
        player_names = ", ".join(p.name for p in room.players)
        print("请选择两个角色，请勿重复选择")
        player1_name = input(f"玩家1请从以下角色中选择：{player_names}")
        player2_name = input(f"玩家2请从以下角色中选择：{player_names}")
        for p in room.players:
            if p.name == player1_name:
                player1 = p
                break
        for p in room.players:
            if p.name == player2_name:
                player2 = p
                break
        for _ in range(5):
            result = player1.fight(player2)
            print(result)
            if player1.wins == 2:
                print(f"最终结果 {player1.name} 胜出")
                break
            elif player2.wins == 2:
                print(f"最终结果 {player2.name} 胜出")
                break
        else:
            print("最终结果是平局")
    elif choice == "5":
        print("退出程序")
        break
    else:
        print("输入错误")
